package com.wing.product.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.annotation.RequestObjectFieldIgnore;
import com.wing.common.constant.SecurityConstants;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.product.model.entity.WarehouseStorageOrder;
import com.wing.product.model.request.WarehouseStorageOrderForm;
import com.wing.product.model.response.WarehouseOutboundRecordVO;
import com.wing.product.model.response.WarehouseStorageOrderVO;
import com.wing.product.service.WarehouseStorageOrderService;
import com.wing.web.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value = "托管入库管理", tags = "#/warehouse/product/warehouseStorageRecord")
@RestController("WarehouseStorageRecordController")
@RequestMapping("/warehouse/storageRecord")
@RequiredArgsConstructor
public class WarehouseStorageOrderController {

    private final WarehouseStorageOrderService warehouseStorageRecordService ;


    @ApiOperation(value = "查询所有", notes = "搜索")
    @GetMapping("/list")
    public PageResult<WarehouseStorageOrderVO> list(
            @RequestParam(value = "page", defaultValue = "1", required = false) Integer page
            ,@RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit
            ,@RequestParam(value = "sort", defaultValue = "create_time", required = false) String sort
            ,@RequestParam(value = "order", defaultValue = "asc", required = false) String order,
            String warehouseFactoryAdministrationId,
            String warehouseCustomerId,
            String warehouseOrderCode,
            String signCode,
            String waybill,
            Long customerId,
            Integer status
    ){
        if(JwtUtils.getClientId().equals(SecurityConstants.WAREHOUSE_CLIENT_ID)){
                customerId = JwtUtils.getDepartmentId();
        }
        Page<WarehouseStorageOrder> pg = new Page<>(page, limit);
        Map paramMap=new HashMap();
        paramMap.put("sort", StrUtil.toUnderlineCase(sort));
        paramMap.put("order", order);
        paramMap.put("warehouseFactoryAdministrationId", warehouseFactoryAdministrationId);
        paramMap.put("warehouseCustomerId", warehouseCustomerId);
        paramMap.put("warehouseOrderCode", warehouseOrderCode);
        paramMap.put("signCode", signCode);
        paramMap.put("waybill", waybill);
        paramMap.put("customerId", customerId);
        paramMap.put("status", status);
        //搜索
        IPage<WarehouseStorageOrderVO> list = warehouseStorageRecordService.pageList4app(pg,paramMap);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

//    @ApiOperation(value = "添加入库作业", notes = "添加")
//    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
//    @PostMapping("/add")
//    public JsonResult add(@RequestBody WarehouseStorageOrderForm warehouseStorageOrderForm)  {
//        try{
//            return warehouseStorageRecordService.saveWarehouseStorageRecord(warehouseStorageOrderForm);
//        }catch (Exception e){
//            return JsonResult.fail(e.getMessage());
//        }
//    }

    @ApiOperation(value = "添加入库作业", notes = "添加")
    @PostMapping("/add")
    public JsonResult add(@RequestBody WarehouseStorageOrderForm warehouseStorageOrderForm)  {
        try{
            return warehouseStorageRecordService.save(warehouseStorageOrderForm);
        }catch (Exception e){
            return JsonResult.fail(e.getMessage());
        }
    }


    @ApiOperation(value = "查询入库明细", notes = "查询")
    @GetMapping ("/selectStorageRecordDetail")
    public JsonResult<WarehouseStorageOrderVO> selectStorageRecordDetail(@RequestParam String id){
        if (!StringUtils.isEmpty(id)){
            WarehouseStorageOrderVO warehouseStorageOrderVO = warehouseStorageRecordService.selectStorageRecordDetail(id);
            if (ObjectUtil.isNotEmpty(warehouseStorageOrderVO)){
                return  JsonResult.success("请求成功",warehouseStorageOrderVO);
            }else {
                return  JsonResult.success("数据为空",warehouseStorageOrderVO);
            }
        }else {
            return  JsonResult.fail("id为空");
        }
    }

    @ApiOperation(value = "根据捆包码查询详情", notes = "查询")
    @GetMapping ("/findFactoryByUserBundleCode")
    public JsonResult<WarehouseStorageOrderVO> findFactoryByUserBundleCode(@RequestParam String userBundleCode){
        if (!StringUtils.isEmpty(userBundleCode)){
            WarehouseStorageOrderVO warehouseStorageOrderVO = warehouseStorageRecordService.findFactoryByUserBundleCode(userBundleCode);
            if (ObjectUtil.isNotEmpty(warehouseStorageOrderVO)){
                return  JsonResult.success("请求成功",warehouseStorageOrderVO);
            }else {
                return  JsonResult.success("数据为空",warehouseStorageOrderVO);
            }
        }else {
            return  JsonResult.fail("id为空");
        }
    }

    @ApiOperation(value = "查询所有捆包码", notes = "查询")
    @GetMapping ("/findSign")
    public JsonResult findSign(){
        List<WarehouseStorageOrderVO> warehouseStorageOrderVOList =warehouseStorageRecordService.findSign();
            return JsonResult.success(warehouseStorageOrderVOList);
    }



    @ApiOperation(value = "详情", notes = "详情")
    @GetMapping ("/info")
    public JsonResult info(String id){
        WarehouseStorageOrderVO warehouseStorageOrderVO =warehouseStorageRecordService.info(id);

        return JsonResult.success(warehouseStorageOrderVO);
    }



}
